using System.Linq;

namespace Yonkly.Models
{
    public static class GroupViewDataExtensions
    {
        public static bool CanView(this GroupViewData group)
        {
            //if public group then anyone can view
            if (group.GroupInfo.Visibility == (int)GroupVisibilities.Public)
                return true;
            //if private then only members can view it
            if (!group.IsAuthenticated)
                return false;
            return group.ApprovedGroupMembers.Count(
                       mem => mem.UserId == group.LoggedInUser.UserId) > 0;
        }

        public static bool CanJoin(this GroupViewData group)
        {
            //only logged in users can join
            if (!group.IsAuthenticated)
                return false;

            //only non members and members who haven't already tried to join
            //can join open groups
            int countForLoggedInUser;
            using (var dc = new YonklyDataContext())
            {
                countForLoggedInUser = dc.GroupMembers.Count(
                    mem => mem.UserId == group.LoggedInUser.UserId
                           && mem.GroupId == group.GroupInfo.Id);
            }
            //already a member
            if (countForLoggedInUser > 0)
                return false;

            //return true if group is public and open
            if (group.GroupInfo.Visibility == (int)GroupVisibilities.Public &&
                group.GroupInfo.Membership == (int)GroupMemberships.Open)
                return true;

            //should never happen
            return false;
        }

        public static bool CanLeave(this GroupViewData group)
        {
            //if approved member and not owner then you can leave
            return group.MyGroupRole != GroupMemberRoles.Owner &&
                   group.ApprovedGroupMembers.Count(approvedMember =>
                                                    approvedMember.UserId == group.LoggedInUser.UserId) > 0;
        }

        public static bool CanManageMembers(this GroupViewData group)
        {
            //only logged in users can manage
            if (!group.IsAuthenticated)
                return false;

            //if logged in user is admin and approved 
            if (group.ApprovedGroupMembers.Count(mem => mem.UserId == group.LoggedInUser.UserId
                                                        && (mem.Role == GroupMemberRoles.Owner)) > 0)
            {
                return true;
            }
            return false;
        }

        public static bool CanPostMessage(this GroupViewData group)
        {
            if (!group.IsAuthenticated)
                return false;
            if (group.ApprovedGroupMembers.Count(mem => mem.UserId == group.LoggedInUser.UserId
                                                        && mem.Status == GroupMemberStatuses.Approved) > 0)
            {
                return true;
            }
            return false;
        }


        public static GroupMember MyGroupSettings(this GroupViewData group)
        {
            GroupMember query;
            using (var dc = new YonklyDataContext())
            {
                query = (from g in dc.GroupMembers
                         where g.GroupId == @group.GroupInfo.Id &&
                               g.UserId == @group.LoggedInUser.UserId
                         select g).Single();


            }
            return query;
        }
    }
}